﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FTchina.SMSServer
{
    /// <summary>
    ///将短信进行分割
    /// </summary>
    public class SMSSplite
    {
        public SMSSplite()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        private const String PRE_STR = "";//(续)

        private const String SUF_STR = "";//(待续)

        private static int PRE_STR_LEN = PRE_STR.Length;

        private static int SUF_STR_LEN = SUF_STR.Length;

        private static int EXT_LENGTH = PRE_STR_LEN + SUF_STR_LEN;

        private static int MAX_LEN = 70; //每个短信最长大小

        /// <summary>
        /// 切割字符串，使每串不超过70个字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public List<String> StringSplit(String str)
        {
            List<String> msms = new List<string>();
            bool star = true;
            while (true)
            {
                if (!star)
                {
                    str = PRE_STR + str;
                }
                int lenght = str.Length;
                int msgCoun = getMsgCount(lenght);
                int nullSpace = getLastMsgLeftCount(lenght, msgCoun);
                if (str.Length <= MAX_LEN)
                {
                    //不用分割
                    msms.Add(str);
                    break;
                }
                else
                {
                    int splitIndex = getSplitIndex(str, nullSpace);
                    string tempStr = str.Substring(0, splitIndex);
                    msms.Add(tempStr + SUF_STR);
                    str = str.Substring(splitIndex);
                }
                star = false;
            }
            return msms;
        }

        /// <summary>
        /// 获取一条短信最小可以分多少条短信
        /// </summary>
        /// <param name="t">短信长度</param>
        /// <returns></returns>
        private int getMsgCount(int t)
        {
            if (t < MAX_LEN)
                return 1;
            int n = (int)((t + MAX_LEN - 2 * EXT_LENGTH - 1) / (MAX_LEN - EXT_LENGTH));
            return n;
        }

        /// <summary>
        /// 计算最后一条信息还可添加多少个字符
        /// </summary>
        /// <param name="t">短信总字符数</param>
        /// <param name="n">短信理想情况分页数</param>
        /// <returns></returns>
        private int getLastMsgLeftCount(int t, int n)
        {
            if (t <= MAX_LEN)
                return MAX_LEN - t;
            int l = MAX_LEN + (MAX_LEN - EXT_LENGTH) * (n - 1) - t;
            return l;
        }

        private static char[] splitChar = new char[] { ',', ' ', '.', ';', '!', ')', '>', '}', ']', '，', '　', '。', '；', '！', '）', '》', '\n', '\t' };

        /// <summary>
        /// 比较智能地去判断分割的位置，尽量在有分隔符的地方分割
        /// </summary>
        /// <param name="str">str的长度一定大于MAX_LEN</param>
        /// <param name="space">剩余可用空间大小</param>
        /// <returns></returns>
        private int getSplitIndex(String str, int space)
        {
            if (space > 0)
            {
                for (int i = MAX_LEN - SUF_STR_LEN - 1, d = (MAX_LEN - SUF_STR_LEN - space - 1); i >= d; --i)
                {
                    foreach (char c in splitChar)
                    {
                        if (str[i] == c)
                        {

                            //匹配
                            //Console.WriteLine(i + 1);
                            return i + 1;
                        }
                    }
                }
            }
            return MAX_LEN - SUF_STR_LEN;
        }
    }

}